package com.yonyou.crm.bpub.template.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.bpub.template.rmitf.IPCTemplatesRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sys.modules.constants.ClientType;
import com.yonyou.crm.sys.modules.constants.EnumSource;
import com.yonyou.crm.sys.modules.constants.LayoutType;
import com.yonyou.crm.sys.modules.entity.BlockItemVO;
import com.yonyou.crm.sys.modules.entity.BlockVO;
import com.yonyou.crm.sys.modules.entity.FieldVO;
import com.yonyou.crm.sys.modules.entity.LayoutVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutItemVO;
import com.yonyou.crm.sys.modules.entity.ListLayoutVO;
import com.yonyou.crm.sys.modules.entity.MtObjTypeEnum;
import com.yonyou.crm.sys.modules.entity.RelationLayoutVO;

@RequestMapping("/*/{module}/templates")
@RestController
public class TemplateCtrl {
	
	@Autowired
	IPCTemplatesRmService service;
	
	@RequestMapping(method = RequestMethod.GET)
	public Object getTemplate(@PathVariable("module") String module,
			HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Map<String, Object> param = convertParam(paramObj, module);
		Map<String, Object> result = new HashMap<String, Object>();
		if(param.get("layoutType") != null){
			LayoutVO layOut = service.getLayout(param);
			result = layOutToPCLayout(layOut);
			return ResponseUtil.getObject(result);
		}else{
			ListLayoutVO listLayOut = service.getListLayout(param);
			result = listLayOutToPCListLayout(listLayOut);
			return ResponseUtil.getObject(result);
		}
	}
	
	public Map<String, Object> convertParam(JSONObject paramObj, String module){
		Map<String, Object> param = new HashMap<String, Object>();
		Long moduleId = null;
		switch (module) {
		case "customer":
			moduleId = MtObjTypeEnum.CUSTOMER.getValue();
			break;
		case "opportunities":
			break;
		case "contacts":
			break;
		case "leads":
			break;
		default:
		}
		String layoutType = "";
		Integer layoutTypeId = null;
		if (paramObj.get("layoutType") != null) {
			layoutType  = paramObj.getString("layoutType");
		}
		if(layoutType.equals(LayoutType.EDIT.getName())){
			layoutTypeId = LayoutType.EDIT.getIndex();
		}else if(layoutType.equals(LayoutType.VIEW.getName())){
			layoutTypeId = LayoutType.VIEW.getIndex();
		}else{
			layoutTypeId = null;
		}
		String biztypeId = "";
		if (paramObj.get("biztypeId") != null) {
			biztypeId = String.valueOf(paramObj.get("biztypeId"));
		}
		String clientType = "";
		Integer clientTypeId = null;
		if (paramObj.get("clientType") != null) {
			clientType  = paramObj.getString("clientType");
		}
		if(clientType.equals(ClientType.PC.getName())){
			clientTypeId = ClientType.PC.getIndex();
		}else if(clientType.equals(ClientType.MOBILE.getName())){
			clientTypeId = ClientType.MOBILE.getIndex();
		}
		param.put("module",moduleId);
		param.put("biztypeId",biztypeId);
		param.put("layoutType", layoutTypeId);
		param.put("clientType", clientTypeId);
		return param;
	}
	
	/**
	 * 新增/编辑和详情模板
	 * 将查询出来的结果转化为前端需要的结构
	 * @param layOut
	 * @return
	 */
	private Map<String, Object> layOutToPCLayout(LayoutVO layOut){
		if(layOut == null) return null;
		List<Map<String, Object>> blockList = new ArrayList<Map<String,Object>>();
		List<BlockVO> blocks = layOut.getMainLayout();
		if(blocks != null && blocks.size() > 0){
			for(BlockVO block : blocks){
				Map<String, Object> blockMap = new HashMap<String, Object>();
				blockMap.put("blockName", block.getName());
				List<Map<String, Object>> fieldList = new ArrayList<Map<String,Object>>();
				List<BlockItemVO> blockItems = block.getItems();
				if(blockItems != null && blockItems.size() > 0){
					for(BlockItemVO blockItem : blockItems){
						Map<String, Object> blockItemMap = new HashMap<String, Object>();
						FieldVO field = blockItem.getActualField();
						if(field == null) continue;
						blockItemMap.put("fieldName", blockItem.getName());
						blockItemMap.put("fieldApiName", blockItem.getApiName());
						blockItemMap.put("isBlank", blockItem.getIsBlank());
						blockItemMap.put("isRequired", blockItem.getIsRequired());
						blockItemMap.put("isReadonly", blockItem.getIsReadonly());
						blockItemMap.put("width", blockItem.getWidth());
						blockItemMap.put("type", field.getType());
						blockItemMap.put("pcUIType", field.getPcUiType());
						if(field.getEnumSource() == EnumSource.SysEnum){
							blockItemMap.put("enumList", field.getEnums());
							blockItemMap.put("displayName", blockItem.getApiName() + "Name");
						}else if(field.getEnumSource() == EnumSource.RefDoc){
							Map<String, Object> refDoc = field.getRefDoc();
							if(refDoc == null) continue;
							blockItemMap.put("enumList", refDoc.get("items"));
							blockItemMap.put("displayName", blockItem.getApiName() + "Name");
						}else{
							blockItemMap.put("enumList", null);
							blockItemMap.put("displayName", blockItem.getApiName());
						}
						fieldList.add(blockItemMap);
					}
				}
				blockMap.put("fieldList", fieldList);
				blockList.add(blockMap);
			}
		}
		List<RelationLayoutVO> relationLayouts = layOut.getRelationLayout();
		List<Long> relObjIds = new ArrayList<Long>();
		if(relationLayouts != null && relationLayouts.size() > 0){
			for(RelationLayoutVO relationLayout : relationLayouts){
				relObjIds.add(relationLayout.getRelObjId());
			}
		}
		//组装最后结果集
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("relationObject", relObjIds);
		result.put("mainObject", blockList);
		return result;
	}
	
	/**
	 * 列表模板
	 * 将查询出来的结果转化为前端需要的结构
	 * @param listLayout
	 * @return
	 */
	private Map<String, Object> listLayOutToPCListLayout(ListLayoutVO listLayout){
		if(listLayout == null) return null;
		List<Map<String, Object>> fieldList = new ArrayList<Map<String,Object>>();
		List<ListLayoutItemVO> items = listLayout.getItems();
		if(items != null && items.size() > 0){
			for(ListLayoutItemVO item : items){
				Map<String, Object> itemMap = new HashMap<String, Object>();
				FieldVO field = item.getActualField();
				if(field == null) continue;
				itemMap.put("title", item.getName());
				itemMap.put("dataIndex", item.getApiName());
//				itemMap.put("isBlank", item.getIsBlank());
//				itemMap.put("width", item.getWidth());
//				itemMap.put("type", field.getType());
//				if(field.getEnumSource() == EnumSource.SysEnum){
//					itemMap.put("enumList", field.getEnums());
//				}else if(field.getEnumSource() == EnumSource.RefDoc){
//					Map<String, Object> refDoc = field.getRefDoc();
//					if(refDoc == null) continue;
//					itemMap.put("enumList", refDoc.get("items"));
//				}else{
//					itemMap.put("enumList", null);
//				}
				fieldList.add(itemMap);
			}
		}
		//组装最后结果集
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("mainObject", fieldList);
		return result;
	}
}
